<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>jquery实现下拉加载</title>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }
    
    html,
    body {
      height: 100%;
    }
    
    #box {
      height: 100%;
      text-align: center;
      overflow: scroll;
    }
    
    #list>div {
      width: 100%;
      height: 30px;
      line-height: 30px;
    }
    
    #loading {
      height: 32px;
      line-height: 32px;
    }
  </style>
  <script src="/learn/js/jquery.js"></script>
</head>

<body>
  <div id="box">
    <div id="list"></div>
    <div id="loading">正在加载...</div>
  </div>
  <script>
    //状态标记
    var loading = true;
    $(function() {
      loadData();
      
      $('#box').scroll(function() {
        var range = 40;
        var t = $('#box').scrollTop();
        var h = $(window).height();
        var scrollHeight = $('#list').height();
        var totalHeight = parseFloat(h) + parseFloat(t);
        if (scrollHeight - totalHeight < range) {
          if (loading) return;
          loading = true;
          $('#loading').show();
          loadData();
        }
      })

      function loadData() {
        var html = '';
        for (var i = 1; i < 30; i++) {
          html += '<div>' + i + '</div>';
        }
        setTimeout(function() {
          $('#loading').hide();
          $('#list').append(html);
          loading = false;
        }, 600)
      }
    })
  </script>
</body>

</html>